package org.dinnermate.validation;

import java.util.Vector;
import java.util.regex.Pattern;

public class Validator
{
	private Vector<String> regexs;
	private Pattern[] patterns;
	private boolean isReady;
	
	public Validator()
	{
		regexs = new Vector<String>();
		isReady = false;
	}
	
	public void setRegexs(String[] regexs)
	{
		this.regexs.clear();
		
		for (String regex : regexs)
		{
			this.regexs.add(regex);
		}
		
		isReady = false;
	}
	
	public void addRegex(String regex)
	{
		regexs.add(regex);
		isReady = false;
	}
	
	private void compile()
	{
		patterns = new Pattern[regexs.size()];
		
		for (int x = 0 ; x < regexs.size() ; x++)
		{
			patterns[x] = Pattern.compile(regexs.get(x));
		}
		
		isReady = true;
	}
	
	public boolean validate(String input)
	{
		if (!isReady) compile();
		
		for (Pattern p : patterns)
		{
			if (p.matcher(input).matches())
				return true;
		}
		
		return false;
	}
}
